package com.hotel.hotelsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.status.ResultMsg;
import com.common.status.StatusCode;
import com.common.utils.ResultUtils;
import com.common.utils.ResultVo;
import com.hotel.customersystem.pojo.CustomerOrder;
import com.hotel.hotelsystem.mapper.RoomInfoMapper;
import com.hotel.hotelsystem.mapper.RoomMapper;
import com.hotel.hotelsystem.pojo.Room;
import com.hotel.hotelsystem.pojo.RoomInfo;
import com.hotel.hotelsystem.service.RoomInfoService;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (RoomInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-09-30 18:24:09
 */
@Service("roomInfoService")
public class RoomInfoServiceImpl implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomMapper roomMapper;

    // 查询指定日期当中可以订阅的房间号
    @Override
    public ResultVo<HashMap<String, Object>> selectTimeAndState(LocalDateTime startTime, LocalDateTime endTime, int hotelId) {
        QueryWrapper<RoomInfo> qw = new QueryWrapper<>();
        qw.eq("in_time",startTime);
        qw.eq("out_time",endTime);
        qw.eq("hotel_id",hotelId);
        qw.ne("occupy_state",1);
        List<RoomInfo> roomInfos = roomInfoMapper.selectList(qw);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rooms",roomInfos);
        System.out.println("测试map: "+map);
        if (map.size() != 0){
            //存在空闲房间
            ResultVo<HashMap<String, Object>> success = ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE, map);
            return success;
        }
        return ResultUtils.error();
    }

    @Override
    public RoomInfo selectByOrderId(Integer orderId) {
        QueryWrapper<RoomInfo> wq1 = new QueryWrapper<>();
        wq1.eq("order_id",orderId);
        return roomInfoMapper.selectOne(wq1);
    }

    @Override
    public Integer updateByOrderId(RoomInfo roomInfo) {
        return roomInfoMapper.updateById(roomInfo);
    }

    @Override
    public Integer deleteByOrderId(Integer orderId) {
        QueryWrapper<RoomInfo> wq1 = new QueryWrapper<>();
        wq1.eq("order_id",orderId);
        return roomInfoMapper.delete(wq1);
    }

    @Override
    public Boolean haveFitRoom(CustomerOrder order,RoomInfo roomInfo) {
        //查询房间类型相符的房间
        QueryWrapper<Room> qw = new QueryWrapper<>();
        qw.eq("room_status",0);
        qw.eq("room_type_id",roomInfo.getRoomTypeId());
        qw.eq("hotel_id",roomInfo.getHotelId());
        List<Room> rooms = roomMapper.selectList(qw);
        System.out.println("***rooms: "+rooms);

        //查询可使用的房间
        List<Room> fitRooms = new ArrayList<>();
        for(Room room:rooms){
            int roomNum = room.getRoomNum();
            System.out.println("***roomNum: "+roomNum);
            QueryWrapper<RoomInfo> qw1 = new QueryWrapper<>();
            qw1.eq("room_num",roomNum);
            qw1.and(wrapper->
                    wrapper.and(wrapper1->
                                    wrapper1.lt("in_time",order.getCheakInDate()).gt("in_time",order.getCheakOutDate()))
                            .or(wrapper2->
                                    wrapper2.lt("out_time",order.getCheakInDate()).gt("out_time",order.getCheakOutDate()))
                            .or(wrapper3->
                                    wrapper3.eq("in_time",order.getCheakInDate()).eq("out_time",order.getCheakOutDate())));

            List<RoomInfo> roomInfoList = roomInfoMapper.selectList(qw1);//查询该房间号在用户住房期间是否被占用
            System.out.println("****roomInfoList: "+roomInfoList);
            if(roomInfoList.size()==0){
                fitRooms.add(room);
            }
        }
        System.out.println("****fitrooms: "+fitRooms);
        if(fitRooms.size()>0){
            return true;
        } else {
            return false;
        }
    }


}

